#!/bin/bash
#
# Wrapper script for llvm-config. Supplies the right environment variables
# for the target and delegates to the native llvm-config for anything else. This
# is needed because arguments like --ldflags, --cxxflags, etc. are set by the
# native compile rather than the target compile.
#
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
NEXT_LLVM_CONFIG="$(which -a llvm-config | sed -n 2p)"
export YOCTO_ALTERNATE_EXE_PATH="${YOCTO_ALTERNATE_EXE_PATH:="$(readlink -f "$SCRIPT_DIR/../llvm-config")"}"
if [ -n "$( echo $base_libdir | sed -n '/lib64/p')" ]; then
    export YOCTO_ALTERNATE_LIBDIR="${YOCTO_ALTERNATE_LIBDIR:="/lib64"}"
else
    export YOCTO_ALTERNATE_LIBDIR="${YOCTO_ALTERNATE_LIBDIR:="/lib"}"
fi
if [[ $# == 0 ]]; then
  exec "$NEXT_LLVM_CONFIG"
fi

remain=""
output=""
for arg in "$@"; do
  case "$arg" in
    --cppflags)
      output="${output} ${CPPFLAGS}"
      ;;
    --cflags)
      output="${output} ${CFLAGS}"
      ;;
    --cxxflags)
      output="${output} ${CXXFLAGS}"
      ;;
    --ldflags)
      output="${output} ${LDFLAGS}"
      ;;
    --shared-mode)
      output="${output} shared"
      ;;
    --link-shared)
      break
      ;;
    *)
      remain="${remain} ${arg}"
      ;;
  esac
done

if [ "${remain}" != "" ]; then
      output="${output} "$("$NEXT_LLVM_CONFIG" ${remain})
fi

echo "${output}"
